/*
	NounCargo.cpp
	(c)2000 Palestar Inc, Richard Lyle
*/


#include "Debug/Assert.h"
#include "Standard/Limits.h"
#include "VerbLoad.h"
#include "NounShip.h"
#include "NounCargo.h"
#include "StructurePort.h"
#include "GameContext.h"

//----------------------------------------------------------------------------

const char *	CARGO_NC = "Misc\\Resource Container\\NC_Resource.prt";

//----------------------------------------------------------------------------

IMPLEMENT_FACTORY( NounCargo, NounGame );
REGISTER_FACTORY_KEY( NounCargo, 4335706563441832149 );

BEGIN_PROPERTY_LIST( NounCargo, NounGame )
	ADD_TRANSMIT_UPDATE_PROPERTY( m_Quantity );
END_PROPERTY_LIST();

NounCargo::NounCargo() : m_Quantity( 1 )
{
	NounContext::Link context;
	if ( loadResource( CARGO_NC, context ) )
		setNounContext( context );
}

//----------------------------------------------------------------------------

void NounCargo::preRender( RenderContext &context, 
					const Matrix33 & frame, 
					const Vector3 & position )
{
	BaseNode * pParent = parent();
	if ( WidgetCast<NounShip>( pParent ) != NULL ||
		WidgetCast<NounPlanet>( pParent ) != NULL ||
		WidgetCast<StructurePort>( pParent ) != NULL )
	{
		// don't render when attached to the planet or a ship
		return;
	}

	Noun::preRender( context, frame, position );
}

//----------------------------------------------------------------------------

NounType NounCargo::nounType() const
{
	return TYPE_LOCAL_OBJECT;
}

bool NounCargo::collidable() const
{
	return true;
}

bool NounCargo::canBeDetected() const
{
	return true;
}

bool NounCargo::isCargo() const
{
	return true;
}

void NounCargo::simulate( dword nTick )
{
	if ( m_Quantity <= 0 )
		setDetach();

	Noun::simulate( nTick );
}

void NounCargo::collision( Noun * pCollide, const Vector3 & intersect )
{
	// NOTE: Decided to disable automatic loading of cargo, since some players wanted to be able
	// to pick which cargo they want to grab after a ship explodes.
	/*
	if ( server() )
	{
		// if a ship, then load the cargo
		if ( WidgetCast<NounShip>( pCollide ) )
		{
			NounShip * pShip = (NounShip *)pCollide;
			if ( pShip->canLoad( this ) )
				Verb::Ref( new VerbLoad( pShip, this ) );
		}
	}
	*/
}

//----------------------------------------------------------------------------

int NounCargo::quantity() const
{
	return m_Quantity;
}

int NounCargo::durability() const
{
	return 50;
}

int NounCargo::value() const
{
	return 0;
}

int NounCargo::size() const
{
	return 1;
}

int NounCargo::maxQuantity() const
{
	return 1000;
}

void NounCargo::setQuantity( int quantity )
{
	m_Quantity = quantity; //Clamp( quantity, 0, maxQuantity() );
}

CharString NounCargo::status() const
{
	return NounGame::status();
}


//----------------------------------------------------------------------------
//EOF
